#!/usr/bin/perl 

use strict;
use DBI;
use CGI;
use TTraker;

my $dbh;
initialize_dbi();

my $form_ref = &parse_input;
print_header();

my %FORM_DATA = %$form_ref; 

my $num = $FORM_DATA{'empNum'};
my $name = $FORM_DATA{'empName'};
my $job = $FORM_DATA{'empJob'};
my $loc = $FORM_DATA{'empLoc'};
my $prmFirst = $FORM_DATA{'prmFirst'};
my $prmLast = $FORM_DATA{'prmLast'};
my $prmLogin = $FORM_DATA{'prmLogin'};
my $prmLocation = $FORM_DATA{'prmLocation'};
my $prmFacility = $FORM_DATA{'prmFacility'};
my $prmPayCat = $FORM_DATA{'prmPayCat'};
my $prmJob = $FORM_DATA{'prmJob'};
my $prmRate = $FORM_DATA{'prmRate'};
my $prmNum = $FORM_DATA{'prmNum'};
my @info;
my $message = "";
my $str="";
if ($prmFirst eq "" || $prmLast eq "" || $prmLogin eq "" || $prmLocation eq "" ||  $prmFacility eq "" || $prmPayCat eq "" || $prmJob eq ""|| $prmRate eq "" || $prmNum eq "" ) {
	$message = "All fields must be filled in!";
	& add_new_perm_msg ($num, $name, $job, $loc, $prmFirst, $prmLast, $prmLogin, $prmLocation, $prmFacility, $prmPayCat, $prmJob, $prmRate, $prmNum, $message );
} else {
	my $status = check_new_perm_status ($prmNum, $prmLogin);
	# print "STATUS IS: $status<br/>";
	if  ($status == 0 ) {
		my $msg = add_new_perm_employee ($prmFirst, $prmLast, $prmLogin, $prmLocation, $prmFacility, $prmPayCat, $prmJob, $prmRate, $prmNum );
		# print 
		pay_activity_screen_from_pm  ($num, $name, $job, $loc, "");
	} elsif ( $status > 0 ) {
		my $info_ref = get_employee_info ($prmLogin);
		@info = @$info_ref;
		my $employee_num = $info[0];
		my $first_name = $info[1];
		my $last_name = $info[2];
		my $job = $info[3];
		my $location = $info[4];
		if ( $status == 1 ) {
			$str = "Employee # already used by ";
		} elsif ($status == 2) {
			$str = "Login ID already used by ";
		} else {
			$str = "Employee # and Login ID already used by ";
		} 			
		$message = "$str $first_name $last_name (employee #: $employee_num, login: $prmLogin)<br/>";
		& add_new_perm_msg ($num, $name, $job, $loc, $prmFirst, $prmLast, $prmLogin, $prmLocation, $prmFacility, $prmPayCat, $prmJob, $prmRate, $prmNum, $message );
	}	
}

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################

sub emp_log_in
{
print <<ENDHEADER;
<html>
<head>
<link rel="stylesheet" type="text/css" href="${css_ref}trak.css" />
<script type="text/javascript" src="${js_ref}trak.js"></script>
<title>trak_01_emp_num.html NO NUM</title>
</head>
<body onload="startTime()">
<h2><center>${banner}</center></h2>
<hr/>
<div id="clock"></div>
<div id="box1">
   &nbsp&nbsp&nbsp&nbsp&nbsp Employee ID: <br/><br/>
</div> 
<form id="box2" autocomplete='off' name="empID" action="${cgi_ref}trak_01_emp_log_in.pl" method="post">
   <input type="text" name=empID />&nbsp&nbsp
   <input type="submit" value="Log Into system">
</form>

</body>
</html>

ENDHEADER
}


sub add_new_perm_msg
{

# print "$_[0], $_[1], $_[2], $_[3], $_[4], $_[5], $_[6], $_[7], $_[8], $_[9], $_[10], $_[11], $_[12], $_[13], $_[14]<br/>"; 

my $num = $_[0];
my $name = $_[1];
my $job = $_[2];
my $loc = $_[3];
my $prmFirst = $_[4];
my $prmLast = $_[5];
my $prmLogin = $_[6];
my $prmLocation = $_[7];
my $prmFacility = $_[8];
my $prmPayCat = $_[9];
my $prmJob = $_[10];
my $prmRate = $_[11];
my $prmNum = $_[12];
my $message = $_[13];

print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_03_adm_add_new_client.pl</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  &nbsp&nbsp&nbsp&nbsp&nbsp
  <div id = "box5_1"> 
  <h3>Add New Permanent Employee</h3>
  </div>
	<form id="box2" autocomplete="off" name="empID" action="${cgi_ref}trak_03_add_new_perm.pl" method="post">
	<input type="hidden" name=empNum value='$num'/>
	<input type="hidden" name=empName value='$name'/>
	<input type="hidden" name=empJob value='$job'/>
	<input type="hidden" name=empLoc value='$loc'/>  
	<table>
		<tr>
			<td>First Name: &nbsp</td>
			<td><input type="text" value = "$prmFirst" size=24 name=prmFirst />&nbsp&nbsp</td>
		</tr>
		<tr>
			<td>Last Name: </td>
			<td><input type="text" value = "$prmLast" size=24 name=prmLast />&nbsp&nbsp</td>
		</tr>
		<tr>
		   <td>Login ID: </td>
		   <td><input type="text" value = "$prmLogin" size=10 name=prmLogin />&nbsp&nbsp</td>
		</tr>
		<tr>
			<td>Location: </td>
			<td><input type="text" value= "$prmLocation" size=5 name=prmLocation />&nbsp&nbsp</td>
		</tr>
		<tr>
   		<td>Facility #:</td>
   		<td><input type="text" value = "$prmFacility" size=5 name=prmFacility />&nbsp&nbsp</td>
   	<tr/>
   	<tr>
	   	<td>Pay Cat: </td>
	   	<td><input type="text" value = "$prmPayCat" size=5 name=prmPayCat />&nbsp&nbsp</td>
		</tr>
		<tr>
	   	<td>Job: </td>
	   	<td><input type="text" value = "$prmJob" size=5 name=prmJob />&nbsp&nbsp</td>
		</tr>
		<tr>
   		<td>Rate: </td>
   		<td><input type="text" size=5 value = "$prmRate" name=prmRate />&nbsp&nbsp<td>
		</tr>
		<tr>
   		<td>Emp #: </td>
   		<td><input type="text" size=5 value = "$prmNum" name=prmNum />&nbsp&nbsp<td>
		</tr>

	</table>	
   	<br/>
   	<input type="submit" value="Add New Permanent">
   	<br/><br/>
 	<a href="${cgi_ref}trak_00_start_up.pl">Return to Log-In</a><br/>
 	<br/><br/>
 	<b id="warn">$message</b><br/><br/>
 </form>
 </div>
 </body>
 </html>
ENDHEADER

}


